package com.jingdianjichi.subject.domain.service.impl;

import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectInfoBOConverter;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeFactory;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeHandler;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Resource
    private SubjectInfoService subjectInfoService;

    @Resource
    private SubjectTypeFactory subjectTypeFactory;

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SubjectInfoBO subjectInfoBO) {
        SubjectInfo subjectInfo = SubjectInfoBOConverter.Instance.BOconverterInfo(subjectInfoBO);

        subjectInfoService.insert(subjectInfo);

        SubjectTypeHandler handler = subjectTypeFactory.getHandler(subjectInfo.getSubjectType());
        subjectInfoBO.setId(subjectInfo.getId());
        handler.add(subjectInfoBO);

        List<Integer> categoryIds = subjectInfoBO.getCategoryIds();
        List<Integer> labelIds = subjectInfoBO.getLabelIds();

        List<SubjectMapping> mappingList = new LinkedList<>();

        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setCategoryId(Long.valueOf(categoryId));
                subjectMapping.setLabelId(Long.valueOf(labelId));
                subjectMapping.setSubjectId(subjectInfoBO.getId());
                subjectMapping.setIsDeleted(IsDeletedFlagEnum.UNDELETED.getCode());
                mappingList.add(subjectMapping);
            });
        });

        subjectMappingService.batchInsert(mappingList);
    }

    @Override
    public PageResult<SubjectInfoBO> getSubjectPage(SubjectInfoBO subjectInfoBO) {
        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBO.getPageNo());
        pageResult.setPageSize(subjectInfoBO.getPageSize());
        SubjectInfo subjectInfo = SubjectInfoBOConverter.Instance.BOconverterInfo(subjectInfoBO);
        int count = subjectInfoService.conditionByCount(subjectInfo, subjectInfoBO.getCategoryId(), subjectInfoBO.getLabelId());
        int start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();
        if (count == 0) {
            return pageResult;
        }
        pageResult.setTotal(count);
        List<SubjectInfo> subjectInfoList = subjectInfoService.queryPage(subjectInfo, subjectInfoBO.getCategoryId(), subjectInfoBO.getLabelId(), start, subjectInfoBO.getPageSize());
        List<SubjectInfoBO> subjectInfoBOS = SubjectInfoBOConverter.Instance.ListEntityToBO(subjectInfoList);
        pageResult.setRecords(subjectInfoBOS);
        return pageResult;
    }

    @Override
    public SubjectInfoBO querySubjectInfo(SubjectInfoBO subjectInfoBO) {

        SubjectInfo subjectInfo = SubjectInfoBOConverter.Instance.BOconverterInfo(subjectInfoBO);

        SubjectInfo info = subjectInfoService.querySubjectInfo(subjectInfo);

        SubjectTypeHandler handler = subjectTypeFactory.getHandler(info.getSubjectType());
        SubjectOptionBO optionBO = handler.query(info.getId().intValue());

        SubjectInfoBO bo = SubjectInfoBOConverter.Instance.EntityconverterBO(info);
        bo.setSubjectAnswer(optionBO.getSubjectAnswer());
        bo.setSubjectName(info.getSubjectName());
        bo.setSubjectDifficult(info.getSubjectDifficult());

        //转换
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setSubjectId(info.getId());
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<String> labelNameList = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
        bo.setLabelName(labelNameList);
        return bo;
    }
}
